package com.haojiang.mh.remote.widget

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.haojiang.mh.R
import com.haojiang.mh.remote.bean.BleRssiDevice
import kotlinx.android.synthetic.main.item_ble_group.view.*


class BleSIngleItemView : RelativeLayout {

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)


    var bleRssiDevice: BleRssiDevice? = null
    var isSelect: Boolean = false

    init {
        View.inflate(context, R.layout.item_ble_single, this)
    }

    fun setData(bleRssiDevice: BleRssiDevice) {
//        val deviceAlias = BleUtil.readDeviceAlias(bleRssiDevice.bleAddress)
//        if(TextUtils.isEmpty(deviceAlias)) {
//            tvDevName.setText(bleRssiDevice.deviceAlias)
//        }else {
//            tvDevName.setText(deviceAlias)
//        }
//        tvDevRssi.setText(data.rssi.toString())
        if(TextUtils.isEmpty(bleRssiDevice.deviceAlias)) {
            tvDevName.setText(bleRssiDevice.bleName)
        }else {
            tvDevName.setText(bleRssiDevice.deviceAlias)
        }
        ivDevRssi.setImageResource(getRssiRes(bleRssiDevice.rssi))
    }


    private fun getRssiRes(rssi: Int): Int {
        when (rssi) {
            in -80..0 -> {
                return R.drawable.rssi4
            }
            in -90..-80 -> {
                return R.drawable.rssi3
            }
            in -100..-90 -> {
                return R.drawable.rssi2
            }
            else -> {
                return R.drawable.rssi1
            }
        }
    }
}


//    fun getRssiDistance(bleRssi: Int): String {
//        val rssi: Int = Math.abs(bleRssi)
////        val power = (rssi - 60) / (10.0 * 3.3)
//        val power = (rssi - 70) / (10.0 * 2.0)
////        val location = Math.pow(10.0, power).toString()
////        return location.substring(0, 6) + "米"
//        return doubleToStr(Math.pow(10.0, power)) + "米"
//    }
//
//    fun doubleToStr(value: Double): String? {
//        var bd = BigDecimal(value)
//        bd = bd.setScale(1, RoundingMode.HALF_UP)
//        return bd.toString()
//    }
//    }